<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
const echarts = require('echarts/lib/echarts')
require('echarts/lib/component/dataset')
require('echarts/lib/component/title')
require('echarts/lib/component/tooltip')
require('echarts/lib/component/grid')
require('echarts/lib/component/dataZoom')
require('echarts/lib/chart/boxplot')
require('echarts/lib/chart/scatter')
import { debounce } from '@/utils'
import { themeColor } from '@/assets/echarts/theme'
const DAY = 1000 * 60 * 60 * 24

export default {
  name: 'AutoAnalyzeWarningboxplotLightVelocity',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    data: {
      type: Object,
      default: () => ({})
    },
    intervalNum: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      chart: null,
      timer: null,
      list: []
    }
  },
  computed: {
    xData() {
      const arr = []
      for (const item in this.data.boxPlotList) {
        arr.push(item * 1000)
      }
      return arr
    },
    // yData() {
    //   return Object.values(this.data.rawList)
    // },
    boxData() {
      return Object.values(this.data.boxPlotList).map(c => ([c.min, c.q1, c.median, c.q3, c.max]))
    },
    outliers() {
      return Object.values(this.data.outlierList).map((c, i) => ([i, c[0]]))
    },
    insiders() {
      return Object.values(this.data.rawList).map((c, i) => ([i, c[0]]))
    }
  },
  watch: {
    data: {
      handler() {
        this.initChart()
        this.chart.on('datazoom', (v) => {
          // if (this.timer) {
          //   clearTimeout(this.timer)
          // }
          // this.timer = setTimeout(() => {
          //   this.$emit('datazoom', v)
          //   this.timer = null
          // }, 500)
          this.$emit('datazoom', v)
        })
      }
    },
    intervalNum: {
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    // this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'themeColor')
      this.chart.setOption({
        color: themeColor,
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          containLabel: false,
          // bottom: 160,
          right: 0
        },
        dataZoom: [
          {
            type: 'slider',
            show: true,
            xAxisIndex: [0],
            start: 0,
            end: 35,
            top: 0
          }
        ],
        xAxis: {
          type: 'category',
          show: false,
          data: this.intervalNum > 0 ? this.xData.filter(v => v <= ((this.intervalNum * DAY) + Math.min(...this.xData))) : this.xData
          // axisLabel: {
          //   interval: 0,
          //   rotate: 45,
          //   formatter: function(value) {
          //     return fullDays(Number(value))
          //   }
          // }
        },
        yAxis: {
          type: 'value'
          // data: this.yData
        },
        series: [
          {
            name: 'boxplot',
            type: 'boxplot',
            data: this.boxData
          },
          {
            name: 'outlier',
            type: 'scatter',
            data: this.outliers,
            itemStyle: {
              color: 'red'
            }
          },
          {
            name: 'outlier',
            type: 'scatter',
            data: this.insiders,
            itemStyle: {
              color: 'green'
            }
          }
        ]
      })
    }
  }
}
</script>
